package com.retail.booking.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.retail.booking.entity.po.BookingSharePosterConfigPo;
import com.retail.booking.entity.request.BookingSharePosterConfigRequest;
import com.retail.booking.entity.vo.BookingSharePosterConfigVo;
import com.retail.booking.mapper.BookingSharePosterConfigMapper;
import com.retail.booking.service.BookingSharePosterConfigService;
import com.retail.core.entity.ParamReq;
import com.retail.core.entity.enums.DelFlagEnum;
import com.retail.core.entity.enums.StatusFlagEnum;
import com.retail.core.entity.result.Result;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 海报配置 Service impl
 *
 * @author Administrator
 */
@Service
public class BookingSharePosterConfigServiceImpl implements BookingSharePosterConfigService {

    @Resource
    private BookingSharePosterConfigMapper bookingSharePosterConfigMapper;

    @Override
    public BookingSharePosterConfigVo getModel(Long id) {
        BookingSharePosterConfigVo bookingSharePosterConfigVo = new BookingSharePosterConfigVo();
        BookingSharePosterConfigPo bookingSharePosterConfigPo = bookingSharePosterConfigMapper.getModel(id);
        BeanUtils.copyProperties(bookingSharePosterConfigPo, bookingSharePosterConfigVo);
        String tag = bookingSharePosterConfigVo.getPosterTag();
        if (ObjectUtil.isNotEmpty(tag)) {
            List<String> tags = Arrays.stream(tag.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
            bookingSharePosterConfigVo.setPosterTags(tags);
        } else {
            bookingSharePosterConfigVo.setPosterTags(new ArrayList<>());
        }
        return bookingSharePosterConfigVo;
    }

    @Transactional
    @Override
    public Long save(BookingSharePosterConfigRequest request) {
        List<String> tags = request.getPosterTags();
        String tagStr = tags.stream().filter(StringUtils::isNotEmpty).collect(Collectors.joining(","));
        request.setPosterTag(tagStr);
        bookingSharePosterConfigMapper.save(request);
        return request.getId();
    }

    @Transactional
    @Override
    public Long update(BookingSharePosterConfigRequest request) {

        List<String> tags = request.getPosterTags();
        String tagStr = tags.stream().filter(StringUtils::isNotEmpty).collect(Collectors.joining(","));
        request.setPosterTag(tagStr);
        bookingSharePosterConfigMapper.update(request);
        return request.getId();
    }

    @Override
    public List<BookingSharePosterConfigVo> listPage(ParamReq<BookingSharePosterConfigRequest> params) {
        List<BookingSharePosterConfigVo> res = bookingSharePosterConfigMapper.listPage(params);

        for (BookingSharePosterConfigVo vo : res) {
            String tag = vo.getPosterTag();
            if (ObjectUtil.isNotEmpty(tag)) {
                List<String> tags = Arrays.stream(tag.split(",")).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
                vo.setPosterTags(tags);
            } else {
                vo.setPosterTags(new ArrayList<>());
            }
            Integer statusFlag = vo.getStatusFlag();
            vo.setStatusFlagName(statusFlag.equals(StatusFlagEnum.USE.getValue()) ? true : false);
        }
        return res;
    }

    @Override
    public Integer countPage(ParamReq<BookingSharePosterConfigRequest> params) {
        return bookingSharePosterConfigMapper.countPage(params);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) {
        return bookingSharePosterConfigMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public Result<Integer> switchStatus(BookingSharePosterConfigRequest request) {
        Integer statusFlag = request.getStatusFlag();
        Long id = request.getId();
        if (ObjectUtil.equal(DelFlagEnum.YES.getValue(), statusFlag)) {
            bookingSharePosterConfigMapper.updateCloseAllStatus();
        }
        Integer res = bookingSharePosterConfigMapper.updateStatusFlagById(id, statusFlag);
        return Result.success(res);
    }
}
